<template>
  <a-modal
    title="添加新考生"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="close"
  >
    <a-spin :spinning="confirmLoading">
      <sign-up-form ref="form"></sign-up-form>
    </a-spin>
  </a-modal>
</template>

<script>
import av from '../../api/av'
import SignUpForm from './SignUpForm'

export default {
  components: { SignUpForm },
  data () {
    return {
      id: '',
      type: 'add',
      visible: false,
      confirmLoading: false
    }
  },
  computed: {
    isAdd () {
      return this.type === 'add'
    }
  },
  methods: {
    add () {
      this.type = 'add'
      this.visible = true
    },
    handleSubmit () {
      const { form: { validateFields } } = this.$refs.form
      validateFields((errors, values) => {
        if (!errors) {
          this.confirmLoading = true
          const text = this.isAdd ? '新增' : '编辑'
          const service = this.isAdd ? av.add('jobType', values) : av.update('jobType', values, this.id)
          service.then(res => {
            this.close()
            this.$message.success(text + '成功')
            this.$emit('ok', values)
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    },
    close () {
      this.visible = false
      this.confirmLoading = false
      this.$refs.form.form.resetFields()
    },
    edit (record) {
      this.id = record.objectId
      this.type = 'edit'
      this.visible = true
      const { form: { setFieldsValue } } = this.$refs.form
      this.$nextTick(() => {
        setFieldsValue(record)
      })
    }
  }
}
</script>
